﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Entity;
using Business;

namespace WebApp
{
    public partial class ServiceforAdmin : System.Web.UI.Page
    {
        SqlConnection con;
        SqlCommand cmd;
        SqlDataAdapter da;
        SqlDataReader dr;
        DataSet ds;
        string strCon;

        protected void Page_Load(object sender, EventArgs e)
        {
            strCon = "Server =localhost;database=ProjectSem3;trusted_connection=true";
            con = new SqlConnection(strCon);
            ds = new DataSet();
            if (!IsPostBack)
                BindingService();
        }
        private void BindingService()
        {
            GridView1.DataSource = Source();
            GridView1.DataBind();
        }
        private DataTable Source()
        {
            Services_Bussiness serB = new Services_Bussiness();
            DataTable ta = new DataTable();
            try
            {
                ta = serB.Binding();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                serB = null;
            }
            return ta;
        }

        protected void btnclear_Click(object sender, EventArgs e)
        {
            TextBox2.Text = "";
            TextBox3.Text = "";
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = int.Parse(GridView1.Rows[e.RowIndex].Cells[0].Text);
            Services_Bussiness sb = new Services_Bussiness();
            try
            {
                sb.Delete(id);
                Label2.Text = "Deleted Successfully!";
            }
            catch (Exception ex)
            {
                Label2.Text = ex.Message.ToString();
            }
            finally
            {
                sb = null;
            }
            GridView1.EditIndex = -1;
            BindingService();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindingService();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            BindingService();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Services_Bussiness sb = new Services_Bussiness();
            GridViewRow row = GridView1.Rows[e.RowIndex];
            int serID = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);
            string sername = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
            int serprice = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text);
            int count = 0;
            count = sb.Update(serID, sername, serprice);
            GridView1.EditIndex = -1;
            BindingService();
        }

        protected void btninsert_Click(object sender, EventArgs e)
        {
            Services_Entity se = new Services_Entity();
            Services_Bussiness them = new Services_Bussiness();
           
            se.SerName = TextBox2.Text.Trim();
            se.SerPrice = Convert.ToInt32(TextBox3.Text.Trim());

            them.InsertService(se);
            BindingService();

            TextBox2.Text = "";
            TextBox3.Text = "";
                
        }

        protected void btnsearch_Click(object sender, EventArgs e)
        {
            try
            {
                if (con.State == ConnectionState.Closed)
                    con.Open();
                string q = "select * from serviceprice where sername = '" + txtSearch.Text + "'";
                cmd = new SqlCommand(q, con);
                cmd.CommandType = CommandType.Text;
                da = new SqlDataAdapter(cmd);
                da.Fill(ds,"serviceprice");
                GridView1.DataSource = ds.Tables["serviceprice"];
                GridView1.DataBind();
            }
            catch (SqlException ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                con.Close();
            }

        }


    }
}